import os
from MyConfig import MyConfig

try:
    cur_path = os.path.dirname(os.path.realpath(__file__))
    conf = f"{cur_path}\\consolidation.json"
    cfg = MyConfig(conf)
    hlist:list = cfg.get("handlers",None)
    hdict={}
    for item in hlist:
        hdict[item["key"]]=item["module"]
    pattern=""
    with open("handlerPattern.py",encoding="utf-8",mode="r") as fp:
        pattern=fp.read()
    for key in hdict.keys():
        fname=f"{cur_path}\\handlers2\\{hdict[key]}.py"
        if key!="Рулон" and os.path.isfile(fname):
            print(f"{key} -> {fname}")
            with open(fname,encoding="utf-8",mode="w") as f:
                s=pattern.replace("module_key",key).replace("module_name_var",hdict[key])
                f.write(s)
                f.flush()
                f.close()
                print("\tdone")
except Exception as exp:
    print(f"error: {exp}")